//
//  HMHomeFreeAudioCell.m
//  HowToDo
//
//  Created by 胡坤 on 2017/1/3.
//  Copyright © 2017年 KY. All rights reserved.
//

#import "HMHomeFreeAudioCell.h"
#import "HMListenAnyTimeResModel.h"
#import <Masonry.h>
@interface HMHomeFreeAudioCell ()
//大的播放按钮
@property(nonatomic,strong)UIButton *playBtn;
//存储资源播放按钮的数组
@property(nonatomic,strong)NSMutableArray *contentsBtns;
//存放资源按钮的视图
@property(nonatomic,strong)UIView *btnsView;
@property(nonatomic,strong)UIButton *resourceBtn;
//记录当前选中播放的资源按钮序号
@property(nonatomic,assign)NSInteger  currentResourceIndex;
//记录当前播放状态
@property(nonatomic,assign)BOOL  isPlaying;
@end
@implementation HMHomeFreeAudioCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier])
    {
        [self setUPUI];
    }
    return self;
}

- (void)setUPUI
{
    self.isPlaying = NO;
    self.currentResourceIndex = 0;
    [self.playBtn mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.centerY.equalTo(self.contentView);
         make.trailing.equalTo(self.contentView);
         make.height.width.equalTo(self.contentView.mas_height).multipliedBy(0.5);
     }];
    [self.contentView addSubview:self.btnsView];
    [self.btnsView mas_makeConstraints:^(MASConstraintMaker *make)
     {
         make.top.bottom.equalTo(self.contentView);
         make.left.equalTo(self.contentView).mas_equalTo(10);
         make.width.offset(self.bounds.size.width* 0.7);
     }];
    self.btnsView.layer.masksToBounds = YES;
}

-(void)setFreeAudios:(NSArray<HMListenAnyTimeResModel *> *)freeAudios
{
    _freeAudios = freeAudios;
    if (self.contentsBtns.count >0)
    {
        [self.contentsBtns enumerateObjectsUsingBlock:^(UIView* obj, NSUInteger idx, BOOL * _Nonnull stop)
        {
            [obj removeFromSuperview];
        }];
    }
    self.contentsBtns = nil;
    for (NSInteger i = 0; i< freeAudios.count; i++)
    {
        HMListenAnyTimeResModel *freeAudio = freeAudios[i];
        //通过模型属性给btn创建btn并赋值
        UIButton* btn = [self creatBtnWithFreeAudio:freeAudio];
        btn.tag = i;
        [self.contentsBtns addObject:btn];
    }
    if (self.contentsBtns.count>2)
    {
        [self.contentsBtns mas_distributeViewsAlongAxis:MASAxisTypeVertical withFixedSpacing:2 leadSpacing:20 tailSpacing:20];
        //设置每个按钮左对齐
        [self.contentsBtns enumerateObjectsUsingBlock:^(UIView* view, NSUInteger idx, BOOL * _Nonnull stop)
         {
             [view mas_makeConstraints:^(MASConstraintMaker *make)
              {
                  make.left.equalTo(self.btnsView);
              }];
         }];
    }
}
- (UIButton*)creatBtnWithFreeAudio:(HMListenAnyTimeResModel *)freeAudios
{
    UIButton* btn = [[UIButton alloc]init];
    [btn setTitle:freeAudios.resource_name forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"new_main_audio_jiantou_6x6_"] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor lightGrayColor] forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor orangeColor] forState:UIControlStateSelected];
    btn.titleLabel.font = [UIFont systemFontOfSize:14];
    [btn addTarget:self action:@selector(resourcePlay:) forControlEvents:UIControlEventTouchUpInside];
    [btn sizeToFit];
    [self.btnsView addSubview:btn];
    return btn;
}

- (UIButton *)playBtn{
    if (!_playBtn)
    {
        UIButton* btn = [[UIButton alloc]init];
        [btn setImage:[UIImage imageNamed:@"new_main_audio_play_icon_87x87_"] forState:UIControlStateNormal];
        [btn setImage:[UIImage imageNamed:@"new_main_audio_parse_icon_87x87_"] forState:UIControlStateSelected];
        [btn sizeToFit];
        [self.contentView addSubview:btn];
        [btn addTarget:self action:@selector(bigPalyBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        _playBtn = btn;
    }
    return _playBtn;
}

- (NSMutableArray *)contentsBtns
{
    if (!_contentsBtns)
    {
        _contentsBtns = [NSMutableArray array];
    }
    return _contentsBtns;
}

- (UIView *)btnsView
{
    if (!_btnsView)
    {
        _btnsView = [[UIView alloc]init];

    }
    return _btnsView;
}
- (void)bigPalyBtnAction:(UIButton *)sender
{
    sender.selected = !self.isPlaying;

    self.isPlaying = sender.selected;
    
    self.clickBigPlayButtonBlock(self.currentResourceIndex ,self.isPlaying);
}

- (void)resourcePlay:(UIButton *)sender
{
    
    if (self.resourceBtn == sender) {
        
        if (self.isPlaying) {
            self.currentResourceIndex = sender.tag;
            self.isPlaying = NO;
        }
        
    } else {

        self.currentResourceIndex = sender.tag;
        self.isPlaying = YES;
    }
    
    self.clickBigPlayButtonBlock(self.currentResourceIndex ,self.isPlaying);
}

- (void)setIsPlaying:(BOOL)isPlaying
{
    _isPlaying = isPlaying;
    
    
    
    if (self.contentsBtns.count)
    {
        //  上一次的取消选中
        self.resourceBtn.selected = NO;
        UIButton *btn = self.contentsBtns[self.currentResourceIndex];
        //  点击的按钮选中
        btn.selected = isPlaying;
        
        self.resourceBtn = btn;
    }
    //  设置大按钮的选中状态
    self.playBtn.selected = isPlaying;
    
}
@end
